package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.EnterpriseStateFlowBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EnterpriseStateFlowMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EnterpriseStateFlowCreateDto;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseStateFlowDto;
import com.molichuxing.services.infrastructure.service.EnterpriseStateFlowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业订单状态流转 TODO
 *
 * @Author zoumingyu
 * @Date 2020年06月10日
 */
@Service("enterpriseStateFlowService")
public class EnterpriseStateFlowServiceImpl implements EnterpriseStateFlowService {
    private static final Logger logger = LoggerFactory.getLogger(EnterpriseStateFlowServiceImpl.class);

    @Resource
    EnterpriseStateFlowMapper enterpriseStateFlowMapper;

    /**
     * 新增流转信息
     *
     * @param createDto
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Integer create(EnterpriseStateFlowCreateDto createDto) {
        if (createDto == null || createDto.getOrderCode() == null) {
            logger.error("订单流程参数异常：" + createDto);
            return null;
        }

        logger.info("订单流程参数：" + createDto.toString());
        EnterpriseStateFlowBean record = new EnterpriseStateFlowBean();
        BeanUtils.copyProperties(createDto,record);
        record.setState(createDto.getOrderStatus());
        int res = enterpriseStateFlowMapper.create(record);
        return res;
    }

    @Override
    public List<EnterpriseStateFlowDto> getListByOrderCode(Long orderCode) {
        logger.info("查询订单状态流程：" + orderCode);
        if (orderCode == null) {
            return null;
        }
        List<EnterpriseStateFlowBean> list = enterpriseStateFlowMapper.getListByOrderCode(orderCode);
        List<EnterpriseStateFlowDto> res = Convert.toEnterpriseStateFlowDto(list);
        return res;
    }
}
